cimport c_cxcore
cimport c_highgui
cimport c_cv

cdef class CvArr:
    ''' Proxy class for the several array types of the OpenCV library.
    Don't work with this class directly. It implements nothing '''
    
    cdef c_cxcore.CvArr* handle(self):
        return self.dummyptr
    
cdef class IplImage(CvArr): 

    #----------- C Methods ---------------------#

    cdef c_cxcore.CvArr* handle(self):
        return <c_cxcore.CvArr*>(self.thisptr)
        
    #----------- Protected Methods -------------#    
    
    def __cinit__(self, needs_free=1):
        ''' sets the flag which determines whether or not to free the image resource during   
        garbage collection. Make sure to set to zero if your not supposed to release the
        the image. '''
        self.needs_free = needs_free
        
    def __dealloc__(self):
        if self.needs_free:
            c_cxcore.cvReleaseImage(&self.thisptr)
          
        
    #----------- Convienience Methods ----------#    
    
    def show(self):    
        ''' a convienience function which displays the image in a new window
        useful for debugging. window exits on keypress. '''
        c_highgui.cvNamedWindow('test', 1)
        c_highgui.cvShowImage('test', self.thisptr)
        c_highgui.cvWaitKey(0)
        c_highgui.cvDestroyWindow('test')        
    
    
    #--------------- Properties ------------------#    
    
    property nSize:
        '''  sizeof(IplImage) '''
        def __get__(self):
            return self.thisptr.nSize    
    
    property ID:
        ''' version (=0) '''
        def __get__(self):
            return self.thisptr.ID            
     
    property nChannels:
        ''' The number of channels. Most OpenCV functions support 1,2,3 or 4 channels '''   
        def __get__(self):
            return self.thisptr.nChannels            
       
    property depth:
        ''' Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S
        IPL_DEPTH_32F and IPL_DEPTH_64F are supported ''' 
        def __get__(self):
            return self.thisptr.depth            
        
    property dataOrder:
        ''' 0 - interleaved color channels, 1 - separate color channels.
        cvCreateImage can only create interleaved images '''
        def __get__(self):
            return self.thisptr.dataOrder
                   
    property origin: 
        ''' 0 - top-left origin, 1 - bottom-left origin (Windows bitmaps style). ''' 
        def __get__(self):
            return self.thisptr.origin            
        
    property width:
        ''' Image width in pixels '''
        def __get__(self):
            return self.thisptr.width            
        
    property height:
        ''' Image height in pixels '''
        def __get__(self):
            return self.thisptr.height    
        
    property imageSize: 
        ''' Image data size in bytes (== image.height * image.widthstep for interleaved data) '''
        def __get__(self):
            return self.thisptr.imageSize            
        
    property widthStep:
        ''' Size of aligned image row in bytes '''
        def __get__(self):
            return self.thisptr.widthStep            
        
    property imageData: 
        ''' Pointer to aligned image data '''
        def __get__(self):
            return self.thisptr.imageData            
        
    property imageDataOrigin:
        ''' Pointer to very origin of image data (not necessarily aligned) 
        needed for correct deallocation '''
        def __get__(self):
            return self.thisptr.imageDataOrigin


cdef class CvMat(CvArr):

    #------------- C Methods ----------------#
    cdef c_cxcore.CvArr* handle(self):
        return <c_cxcore.CvArr*>(self.thisptr)
        
    #--------- Protected Methods ------------#
    def __cinit__(self, needs_free=1):
        self.needs_free = needs_free
        
    def __dealloc__(self):
        if self.needs_free:
            c_cxcore.cvReleaseMat(&self.thisptr)
            
    #-------- Convienience Methods ----------#
    
    #------------ Properties ----------------#    
        
    property type:
        ''' CvMat signature (CV_MAT_MAGIC_VAL), element type and flags '''
        def __get__(self):
            return self.thisptr.type            
        
    property step:
        ''' full row length in bytes '''
        def __get__(self):
            return self.thisptr.step            
       
    property rows:   
        ''' number of rows '''
        def __get__(self):
            return self.thisptr.rows            
        
    property cols: 
        ''' number of columns '''   
        def __get__(self):
            return self.thisptr.cols
            

cdef class CvMatND(CvArr):

    #------------- C Methods ----------------#
    cdef c_cxcore.CvArr* handle(self):
        return <c_cxcore.CvArr*>(self.thisptr)
        
    #--------- Protected Methods ------------#
    def __cinit__(self, needs_free=1):
        self.needs_free = needs_free
        
    def __dealloc__(self):
        if self.needs_free:
            c_cxcore.cvReleaseMatND(&self.thisptr)
            
    #-------- Convienience Methods ----------#
    
    #------------ Properties ----------------#    
        
    property type:
        ''' CvMat signature (CV_MAT_MAGIC_VAL), element type and flags '''
        def __get__(self):
            return self.thisptr.type            
        
    property dims:
        ''' full row length in bytes '''
        def __get__(self):
            return self.thisptr.dims 
            
            
cdef class CvPyramid:
    
    #--------- Protected Methods ------------#
    def __cinit__(self, needs_free=1):
        self.needs_free = needs_free
        
    def __dealloc__(self):
        if self.needs_free:
            c_cv.cvReleasePyramid(&self.thisptr, self.extra_layers)
       

cdef class CvHistogram:
    
    #--------- Protected Methods ------------#
    def __cinit__(self, needs_free=1):
        self.needs_free = needs_free
        
    def __dealloc__(self):
        if self.needs_free:
            c_cv.cvReleaseHist(&self.thisptr)
            
    #-------- Properties --------------------#
    property type:
        def __get__(self):
            return self.thisptr.type
            
    
cdef class CvCapture:

    #---------- Protected Methods -----------#
    
    def __dealloc__(self):
        c_highgui.cvReleaseCapture(&self.thisptr)
        
            
    #--------- convienience functions -------#
    
        
    def setCaptureWidth(self, double width):
        ''' set the capture frame width
        equivalent to cvSetCaptureProperty(CvCapture* capture, 
        CV_CAP_PROP_FRAME_WIDTH, double value) '''
        c_highgui.cvSetCaptureProperty(self.thisptr, 3, width)
    
        
    def setCaptureHeight(self, double height):
        ''' set the capture frame height
        equivalent to cvSetCaptureProperty(CvCapture* capture, 
        CV_CAP_PROP_FRAME_HEIGHT, double value) '''
        c_highgui.cvSetCaptureProperty(self.thisptr, 4, height)
    
    
        
    def queryFrame(self):
        ''' get and return an IplImage from from a capture source
        equivalent to img = cvQueryFrame(CvCapture* capture) '''
        cdef c_cxcore.IplImage* img
        cdef IplImage pyimg = IplImage(needs_free=0)        
        img = c_highgui.cvQueryFrame(self.thisptr)
        pyimg.thisptr = img
        return pyimg
        
    
        
        
            

    
